package com.trtan.hr.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.trtan.hr.pojo.Tree;
import com.trtan.hr.pojo.RespBean;
import com.trtan.hr.pojo.RespPageEntity;
import com.trtan.hr.pojo.Role;
import com.trtan.hr.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author: trtan
 * @create: 2021-03-07 16:05
 * @description: 角色管理
 **/
@RestController
@RequestMapping("/system/role")
public class RoleController {
    private static final Logger logger = Logger.getLogger("RoleController");
    @Autowired
    RoleService roleService;

    @RequestMapping("/getAllRoles")
    public RespBean getAllRoles(@RequestBody JSONObject params) {
        logger.info(params.toString());
        Integer currentPage = params.getInteger("currentPage");
        Integer pageSize = params.getInteger("pageSize");
        String role_id = params.getString("roleName");
        RespBean respBean;
        try {
            RespPageEntity allRoles = roleService.getAllRoles(currentPage, pageSize, role_id);
            respBean = RespBean.ok("", allRoles);
        } catch (Exception e) {
            e.printStackTrace();
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/deleteRole")
    public RespBean deleteRole(@RequestBody JSONObject params) {
        Integer role_id = params.getInteger("role_id");
        RespBean respBean;
        try {
            roleService.deleteRole(role_id);
            respBean = RespBean.ok("删除成功！");
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    private String simpleRoleName(String roleName) {
        roleName = roleName.toLowerCase();
        if (roleName.startsWith("role_")) {
            return roleName.replace("role_", "ROLE_");
        }
        return "ROLE_".concat(roleName);
    }

    @RequestMapping("/addRole")
    public RespBean addRole(@RequestBody JSONObject params) {
        Role role = params.toJavaObject(Role.class);
        role.setName(simpleRoleName(role.getName()));
        RespBean respBean = null;
        try {
            roleService.addRole(role);
            respBean = RespBean.ok("添加成功！", role.getId());
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/modifyRole")
    public RespBean modifyRole(@RequestBody JSONObject params) {
        Role role = params.toJavaObject(Role.class);
        role.setName(simpleRoleName(role.getName()));
        RespBean respBean = null;
        try {
            roleService.modifyRole(role);
            respBean = RespBean.ok("修改成功！");
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/menuAuthorization")
    public RespBean menuAuthorization(@RequestBody JSONObject params) {
        logger.info(params.toString());
        Integer role_id = params.getInteger("role_id");
        List<Tree> arr = params.getJSONArray("menus").toJavaList(Tree.class);
        List<Integer> list = new ArrayList<>();
        if (arr.size() > 0) {
            getMenuTreeIds(arr.get(0), list);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        RespBean respBean = null;
        try {
            roleService.menuAuthorization(role_id, list);
            respBean = RespBean.ok("修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    private void getMenuTreeIds(Tree tree, List<Integer> menus) {
        if (tree == null) {
            return;
        }
        menus.add(tree.getId());
        for(Tree m : tree.getChildren()) {
            getMenuTreeIds(m, menus);
        }
    }

    @RequestMapping("/getRoleTree")
    public RespBean getMenuTree(@RequestBody JSONObject jsonParams) {
        Integer user_id = jsonParams.getInteger("user_id");
        RespBean respBean = null;
        try {
            Map<String, Object> result = roleService.getRoleTree(user_id);
            respBean = RespBean.ok("", result);
        } catch (Exception e) {
            respBean = RespBean.error("获取权限列表失败，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/userAuthorization")
    public RespBean userAuthorization(@RequestBody JSONObject params) {
        logger.info(params.toString());
        Integer user_id = params.getInteger("user_id");
        List<Tree> arr = params.getJSONArray("roles").toJavaList(Tree.class);
        List<Integer> list = new ArrayList<>();
        for (Tree tree : arr) {
            list.add(tree.getId());
        }
        RespBean respBean = null;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        try {
            roleService.userAuthorization(user_id, list);
            respBean = RespBean.ok("修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }
}
